package com.jianghang.class_three.log_format.utils

import com.jianghang.class_three.log_format.utils.ip.IPParser
import com.kumkee.userAgent.{UserAgent, UserAgentParser}
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.{BooleanType, LongType, StringType, StructField, StructType}

/**
  * 输入的日志格式：
  * ip time url method  protocal http_status bytes_sent referer user_agent
  *
  * 转换后数据格式：
  * time ip country province city  method url protocal http_status bytes_sent referer
  * user_agent browser_name browser_version engine_name engine_version os_name platform_name  is_molbie
  *
  **/
object LogConvertUtil {
  //定义输出的字段结构 19
  val structFieldArray = Array(
    StructField("time", StringType),
    StructField("ip", StringType),
    StructField("country", StringType),
    StructField("province", StringType),
    StructField("city", StringType),
    StructField("method", StringType),
    StructField("url", StringType),

    StructField("protocal", StringType),
    StructField("http_status", StringType),
    StructField("bytes_sent", StringType),
    StructField("referer", StringType),

    StructField("user_agent", StringType),
    StructField("browser_name", StringType),
    StructField("browser_version", StringType),
    StructField("engine_name", StringType),
    StructField("engine_version", StringType),
    StructField("os_name", StringType),
    StructField("platform_name", StringType),
    StructField("is_molbie", BooleanType)

    //StructField("day", StringType)
  )

  val struct = StructType(structFieldArray)

  private val userAgentParser = new UserAgentParser()

  /**
    * 根据每一行输入的信息转换为输出的样式
    *
    * @param log_format
    */
  def parseLog(log_format: String): Row = {

    try {
      val splits = log_format.split("\t")

      val time = splits(1)
      val ip = splits(0)
      var country = "-"
      var province = "-"
      var city = "-"
      if (!splits(3).equals("HEAD")) {
        val regionInfo = IPParser.getInstance.analyseIp(ip)
        country = regionInfo.getCountry
        province = regionInfo.getProvince
        city = regionInfo.getCity
//        if (country == null || country.contains("�")) {
//          city = "-"
//          province = "-"
//          country = "-"
//        }
      }
      //ip time url method  protocal http_status bytes_sent referer user_agent
      val url = splits(2)
      val method = splits(3)
      val protocal = splits(4)
      val http_status = splits(5)
      val bytes_sent = splits(6)
      val referer = splits(7)

      val userAgent = splits(8)
      val agent: UserAgent = userAgentParser.parse(userAgent)

      val browser_name = agent.getBrowser
      val browser_version = agent.getVersion
      val engine_name = agent.getEngine
      val engine_version = agent.getEngineVersion
      val os_name = agent.getOs
      val platform_name = agent.getPlatform
      val is_mobile = agent.isMobile


      //val a = browser_name +" "  + engine_name +" " + engine_version +" "+ os_name +" "+ platform_name +" "+ is_mobile +" " + browser_version +" "

      //println(a)

      /**
        * time ip country province city  method url protocal http_status bytes_sent referer
        * user_agent browser_name browser_version engine_name engine_version os_name platform_name  is_molbie
        */
      Row(time, ip, country, province, city, method, url, protocal, http_status, bytes_sent, referer,
        userAgent, browser_name, browser_version, engine_name, engine_version, os_name, platform_name, is_mobile)
    } catch {
      case e: Exception => Row("", "", "", "", "", "", "", "","", "", "", "", "", "", "", "", "", "", "")
    }

  }



}
